//
//  SEDoctorDetailServiceCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/12.
//

import UIKit

enum SEDoctorDetailServiceType: Int {
    case online
    case reserve
}

protocol SEDoctorDetailServiceCellDelegate: NSObjectProtocol {
    func doctorDetailServiceCell(_ cell: SEDoctorDetailServiceCell, didTappedForSwitchService service: SEDoctorDetailServiceType)
}

class SEDoctorDetailServiceCell: UITableViewCell {
    
    weak var delegate: SEDoctorDetailServiceCellDelegate?
    weak var reserveViewDelegate: SEDoctorDetailReserveServiceViewDelegate?
    weak var dateViewDelegate: SERegistrationDoctorDateViewDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
        contentView.backgroundColor = .white
        
        onlineServiceButton.tappedClosure = { [weak self] isSelected in
            guard let self = self else { return }
            self.reserveServiceButton.isSelected = !isSelected
            self.onlineServiceView.isHidden = !isSelected
            self.reserveServiceView.isHidden = isSelected
            self.delegate?.doctorDetailServiceCell(self, didTappedForSwitchService: .online)
        }
        
        reserveServiceButton.tappedClosure = { [weak self] isSelected in
            guard let self = self else { return }
            self.onlineServiceButton.isSelected = !isSelected
            self.reserveServiceView.isHidden = !isSelected
            self.onlineServiceView.isHidden = isSelected
            self.delegate?.doctorDetailServiceCell(self, didTappedForSwitchService: .reserve)
        }
    }
    
    @IBOutlet private weak var onlineServiceButton: SEDoctorDetailServiceButton!
    
    @IBOutlet private weak var reserveServiceButton: SEDoctorDetailServiceButton!
    
    @IBOutlet private weak var onlineServiceView: UIView! {
        didSet {
            onlineServiceView.isHidden = false
            onlineServiceView.layer.cornerRadius = 8
            onlineServiceView.layer.borderWidth = 1
            onlineServiceView.layer.borderColor = UIColor(hex: "#CFF6F9").cgColor
        }
    }
    
    @IBOutlet private weak var reserveServiceView: SEDoctorDetailReserveServiceView! {
        didSet {
            reserveServiceView.dateViewDelagate = self
            reserveServiceView.delegate = self
            reserveServiceView.isHidden = true
        }
    }

    @IBOutlet weak var reserveServeViewHeight: NSLayoutConstraint!

    
    func updateWithViewModel(_ viewModel: SEDoctorDetailViewModel) {
        onlineServiceButton.desc = String(format: "%.2lf/次", (viewModel.model.fee ?? 0) / 100.0)
        reserveServiceButton.desc = ""
        switch viewModel.serviceType {
        case .online:
            onlineServiceButton.isSelected = true
            reserveServiceButton.isSelected = false
            onlineServiceView.isHidden = false
            reserveServiceView.isHidden = true
        case .reserve:
            onlineServiceButton.isSelected = false
            reserveServiceButton.isSelected = true
            onlineServiceView.isHidden = true
            reserveServiceView.isHidden = false
            reserveServiceView.updateData(viewModel)
            reserveServeViewHeight.constant = viewModel.reserveReserveViewHeight
        }
    }
}

// MARK: - SEDoctorDetailReserveServiceViewDelegate

extension SEDoctorDetailServiceCell: SEDoctorDetailReserveServiceViewDelegate {
    func doctorDetailReserveServiceViewSectionHeaderTap() {
        reserveViewDelegate?.doctorDetailReserveServiceViewSectionHeaderTap()
    }
}

extension SEDoctorDetailServiceCell: SERegistrationDoctorDateViewDelegate {
    func registrationDoctorDateViewBtnAction(model: SERegistrationDoctorShift) {
        dateViewDelegate?.registrationDoctorDateViewBtnAction(model: model)
    }
}
